package org.jetlinks.community.device.service.hik;

import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.jetlinks.community.device.entity.LiveStreamParam;
import org.jetlinks.community.device.entity.PlayBackStreamParam;
import org.jetlinks.community.device.enums.ActionEnum;
import org.jetlinks.community.device.enums.CommandHiKEnum;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author LiHongHui
 * @Date 2021/10/19 13:09
 * @Version 1.0
 */
@Service
@Slf4j
public class HiKClient {

    private Map<String, String> getUrl(String target) {
        final String ARTEMIS_PATH = "/artemis";

        return new HashMap<String, String>(2) {
            {
                //根据现场环境部署确认是http还是https
                put("https://", ARTEMIS_PATH + target);
            }
        };
    }

    public String getLiveStream(LiveStreamParam liveStreamParam) {
        String body = JSONObject.toJSONString(liveStreamParam);
        return ArtemisHttpUtil.doPostStringArtemis(getUrl("/api/video/v2/cameras/previewURLs"), body,
            null, null, "application/json" , null);
    }

    /**
     * 获取回放流地址
     * @return : 响应
     */
    public String getBackStream(PlayBackStreamParam playBackStreamParam) {
        String body = JSONObject.toJSONString(playBackStreamParam);
        return ArtemisHttpUtil.doPostStringArtemis(getUrl("/api/video/v2/cameras/playbackURLs"), body,
            null, null, "application/json" , null);
    }

    /**
     * 查询预置点列表
     * @param moId : 视频节点id
     * @return : 响应
     */
    public String presetList(String moId) {
        String contentType = "application/json";
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", moId);
        String body = jsonBody.toJSONString();
        return ArtemisHttpUtil.doPostStringArtemis(getUrl("/api/video/v1/presets/searches"), body,
                null, null, contentType , null);
    }

    /**
     * 云台控制
     * @param moId : 视频节点id
     * @param command ： CommandHiKEnum
     * @param action ： ActionEnum
     * @param presetIndex ： 预置点编号
     * @return : 响应
     */
    public String ptzControl(String moId, CommandHiKEnum command, ActionEnum action, Integer presetIndex) {
        String contentType = "application/json";
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", moId);
        jsonBody.put("command", command.name().toUpperCase());
        jsonBody.put("action", action.getAction());
        jsonBody.put("presetIndex", presetIndex);
        String body = jsonBody.toJSONString();
        return ArtemisHttpUtil.doPostStringArtemis(getUrl("/api/video/v1/ptzs/controlling"), body,
            null, null, contentType , null);
    }
    /**
     * 添加预置点
     * @param moId : 视频节点id
     * @param presetName : 预置点名称
     * @param presetIndex : 预置点
     * @return : 响应
     */
    public String addPresets(String moId, String presetName, Integer presetIndex) {
        String contentType = "application/json";
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", moId);
        jsonBody.put("presetName", presetName);
        jsonBody.put("presetIndex", presetIndex);
        String body = jsonBody.toJSONString();
        String response = ArtemisHttpUtil.doPostStringArtemis(getUrl("/api/video/v1/presets/addition"), body,
            null, null, contentType, null);
        log.info("[海康威视接口]request: moId={},presetName={}, presetIndex={};response={}", moId, presetName, presetIndex, response);
        return response;
    }

    /**
     * 删除预置点列表
     * @param moId : 视频节点id
     * @return : 响应
     */
    public String deletePreset(String moId, Integer presetIndex) {
        String contentType = "application/json";
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", moId);
        jsonBody.put("presetIndex", presetIndex);
        String body = jsonBody.toJSONString();
        return ArtemisHttpUtil.doPostStringArtemis(getUrl("/api/video/v1/presets/deletion"), body,
            null, null, contentType , null);
    }

    /**
     * 获取在线设备列表
     * @param devices
     * @return
     */
    public String getDeviceStatus(List<String> devices) {
        String contentType = "application/json";
        JSONObject jsonBody = new JSONObject();

        jsonBody.put("indexCodes", devices);
        jsonBody.put("status", "1");
        jsonBody.put("pageNo", 1);
        jsonBody.put("pageSize", 500);
        String body = jsonBody.toJSONString();
        return ArtemisHttpUtil.doPostStringArtemis(getUrl("/api/nms/v1/online/camera/get"), body,
            null, null, contentType , null);
    }
}
